#!/bin/bash
set -e

function info() {
echo Usage: `basename $0` '[-s sample_name -i interval] [-t] <fq|bam [...]>'
exit 1
}

while getopts  ":s:p:t:o:i:h" opts
do
    case  $opts  in
		s) sample_name=$OPTARG;;
		p) out_prefix=$OPTARG;;
		t) threads=$OPTARG;;
		o) out_dir=$OPTARG;;
        i) interval=$OPTARG;;
		*) info;;
	esac
done
shift $(($OPTIND - 1))

if [ -z "$1" ]; then info; fi

. $var

if test -z $threads; then
threads=6
fi
# if test -z $out_dir; then
# fastqc.pl -t $threads -o $PWD $@
# fastqc_summ.sh *.zip
# else
# fastqc.pl -t $threads -o $out_dir $@
# fi

if test -n "$interval"; then
    num_interval=`awk 'BEGIN{sum=0}{sum+=($3-$2)}END{print sum}' $interval`
fi
IFS=$'\t \n'
echo -e "$sample_name\t$*" >$out_prefix.reads.list.txt
$java_run/fqstat -i $out_prefix.reads.list.txt -t $threads |sed -e '1s/^#//' >$out_prefix.reads.stat.txt

bases=`tail -1 $out_prefix.reads.stat.txt |cut -f3`

depth=`python -c 'import sys; print(float(sys.argv[1]) / float(sys.argv[2]))' $bases $num_interval`

echo -e "Target\tDepth" > $out_prefix.depth.txt
echo -e "$num_interval\t$depth" >> $out_prefix.depth.txt

cat $out_prefix.reads.stat.txt |cut -f3|awk 'NR>1{if($0/1024/1024 < 1){printf "%.2fkb\n", $0/1024}else if($0/1024/1024/1024 <1){printf "%.2fmb\n", $0/1024/1024}else if($0/1024/1024/1024/1024 <1){printf "%.2fgb\n", $0/1024/1024/1024}else{printf "%.2ftb\n", $0/1024/1024/1024/1024}}' |sed -e '1iData_volumn'>$out_prefix.data.vol.txt

paste $out_prefix.reads.stat.txt $out_prefix.data.vol.txt $out_prefix.depth.txt|sed -e 's/\t/,/g' |csv2xls.pl - $out_prefix.reads.stat.xls

. $cmd_done


